<html>
    <head>
        <title>Photo Album</title>
        <script type="text/javascript" src="./jquery.js"></script>
        <script type="text/javascript">
            jQuery(document).ready(function(){
                jQuery("form[name=form1] input[name=txtUserName]").blur(function(){
                    jQuery.ajax({
                        type:"GET",
                        data:"key="+$("form[name=form1] input[name=txtUserName]").val(),
                        url:"check_exist.php?type=1",
                        success:function(result){
                            $("#spAvailability").html(result);
                        }
                    });
                });
            });
           
            function check_Validation()
            {
                var error = "";
                if(form1.txtUserName.value =="")
                    error += "* User name is mandatory\n";
                else
                    if(! /^[a-z0-9]+$/gi.test(form1.txtUserName.value))
                        error += "* User Name can only alphanumeric\n";
               
                if(form1.txtEmail.value !="" && ! /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(form1.txtEmail.value))
                    error += "* Email isn't correct format\n";
                if(form1.txtPassword.value =="")
                    error += "* Password is mandatory\n";
                else
                    if(! (/[\d]/g.test(form1.txtPassword.value) && /[a-z]/gi.test(form1.txtPassword.value)))
                        error += "* Password must have  both letters and numbers\n";
                if(form1.txtPassword.value != form1.txtRePassword.value)
                    error += "* Password and Repassword not macth\n";
                if(form1.txtFullName.value=="")
                    error +="* Full name is mandatory\n";
                else if(form1.txtFullName.value.length<=6){
                    error +="* Full name must have more than 6 characters\n";
                }

                if(form1.txtPhone.value!="" && ! /[\d]{1,3}-[\d]{1,3}-[\d]{7}/g.test(form1.txtPhone.value)){
                    error+="* Invalid phone number format\n"
                }
                if(error!=""){
                    alert("Please fix the following errors:\n"+error);
                    return false;
                }

                return true;
            }
        </script>
    </head>
    <body>
        <?php
        require 'connect.inc';
        require 'session.inc';
        $res = "";
        $state = 0;
        if ($_SESSION["isLogin"] == 1)
            header("Location: index.php");
        else {
            if (isset($_POST["btnSubmit"])) {
                $username = $_REQUEST["txtUserName"];
                $email = $_REQUEST["txtEmail"];
                $pass = md5($_REQUEST["txtPassword"]);
                $fullname = $_REQUEST["txtFullName"];
                $phone = $_REQUEST["txtPhone"];
                $avatar = "";
                $address = $_REQUEST["txtAddress"];
                $types = array('image/jpeg', 'image/gif', 'image/pjpeg');
                if (count($_FILES) > 0) {
                    $file = $_FILES['fAvatar'];
                    if (!in_array($file['type'], $types)) {
                        return;
                    }
                    if ($file['error'] == 0) {
                        $avatar = $file['name'];
                    }
                }

                if ($phone == "")
                    $phone = "NULL";
                else
                    $phone = "'" . $phone . "'";
                $strSQL = "INSERT INTO USERS(`user_name`, `password`,`email`,`full_name`,`phone`,`join_date`, `avatar`)
					VALUES('$username','$pass','$email','$fullname',$phone,now(),'$avatar')";
                $result = mysql_query($strSQL);
                $idUser = mysql_insert_id();
                if ($result) {
                    $state = 1;
                    $_SESSION["isLogin"] = 1;
                    $_SESSION["iduser"] = $idUser;
                    $_SESSION["username"] = $username;
                    $_SESSION["fullName"] = $fullname;
                    $_SESSION["email"] = $email;
                    //Create user's folder and upload file
                    if (mkdir("upload/$idUser", 0777)) {
                        if (mkdir("upload/$idUser/avatar", 0777)) {
                            if ($avatar != "")
                                move_uploaded_file($file['tmp_name'], "upload/$idUser/avatar/$avatar");
                        }
                    }
                }
            } else {
                $username = "";
                $email = "";
                $fullname = "";
                $address = "";
                $phone = "";
                $avatar = "";

                $res .="<table width='100%' border='1' cellspacing='0'>
                <tbody>
                    <tr></tr>
                    <tr>
                        <td>
                            <img src='logo.jpg'>
                        </td>
                        <td>
                            <h1>Photo Album Website</h1>
                        </td>
                    </tr>
                    <tr>
                        <td width='20%' valign='top'>
                            <a href='index.php'>Home</a>
                            <br>
                            <a href='my_albums.php'>My Albums</a>
                            <br>
                            <a href='users.php'>Other persons</a>
                        </td>
                        <td>
                <center>
                    <h2>User Registration</h2>
                </center>
                <form enctype='multipart/form-data' onsubmit = 'return check_Validation();' method='post' action='register.php' name='form1'>
                    <table>
                        <tbody>
                            <tr>
                                <td>Full name: </td>
                                <td>
                                    <input type='text' value='' name='txtFullName'>
                                    <font color='red'></font>
                                </td>
                            </tr>
                            <tr>
                                <td>Choose avatar: </td>
                                <td>
                                    <input type='file' name='fAvatar'>
                                </td>
                            </tr>
                            <tr>
                                <td>User name: </td>
                                <td>
                                    <input type='text' value='' name='txtUserName' id='txtUserName'> 
                                    <span id='spAvailability' style='color: red'>
                                        <font style='color:red'></font>
                                    </span>
                                </td>
                            </tr>
                            <tr>
                                <td>Password: </td>
                                <td>
                                    <input type='password' name='txtPassword'>
                                    <font color='red'></font>
                                </td>
                            </tr>
                            <tr>
                                <td>Re-type password: </td>
                                <td>
                                    <input type='password' name='txtRePassword'>
                                    <font color='red'></font>
                                </td>
                            </tr>
                            <tr>
                                <td>Email: </td>
                                <td>
                                    <input type='text' value='' name='txtEmail'>
                                   
                                    <font color='red'></font>
                                </td>
                            </tr>
                            <tr>
                                <td>Phone: </td>
                                <td>
                                    <input type='text' placeholder='___-___-_______' value='' name='txtPhone'>
                                    <font color='red'></font>
                                </td>
                            </tr>
                            <tr>
                                <td>Address: </td>
                                <td>
                                    <input type='text' value='' name='txtAddress'>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <input type='submit' name='btnSubmit' value='Submit'>
                                    <input type='reset' name='btnCancel' value='Cancel'>
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </form>
            </td>
            <td width='20%' valign='top'>
                <a href='login.php'>Login</a>
                <a href='register.php'>Register</a>
            </td>
        </tr>
    </tbody>
    </table>";
            }
        }
        if ($state == 1)
            header("Location: index.php");
        else
            echo $res;
        ?>  
    </body>

</html>
